import dayjs from 'dayjs'

export const YmdFormt = 'YYYY-MM-DD'
export const YmFormt = 'YYYY-MM'
export const MdFormt = 'MM-DD'
export const YmdHmsFormt = 'YYYY-MM-DD HH:mm:ss'
export const YmdHmFormt = 'YYYY-MM-DD HH:mm'
export const YmdHFormt = 'YYYY-MM-DD HH'
export const HmFormt = 'HH:mm'

export function formatDataTime (date, format = YmdFormt) {
  if (!date) {
    return ''
  }
  return dayjs(date).format(format)
}

// add方法支持的类型
export const Week = 'week'
export const Day = 'day'
export const Month = 'month'
export const Year = 'year'
export const Hour = 'hour'
export const Minute = 'minute'
export const Second = 'second'
export const MSecond = 'millisecond'

/**
 *
 * @param {*} date 初始时间
 * @param {*} add  增加值 负数为减少
 * @param {*} type
 * @param {*} format 输出类型
 */
export function addDateTime (date, add, type, format = YmdFormt) {
  return dayjs(date).add(add, type).format(format)
}

/**
 *
 * @param {*} date1
 * @param {*} date2
 * @param {*} type
 * @returns  返回2个时间的差 date1-date2
 */
export function diffDateTime (start, end, type = Day) {
  if(type==Day)
  {
    let d1=dayjs(start).startOf('day')
    let d2=dayjs(end).startOf('day')
    return d2.diff(d1, Day)
  }
  return dayjs(end).diff(start, type)
}
